﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace ShapeMergeFactory
{
   internal class BoundingBox
    {
        private double xMin;
        private double yMin;
        private double xMax;
        private double yMax;
        private double zMin;
        private double zMax;
        private double mMin;
        private double mMax;

        public BoundingBox()
        {
            xMin = double.MaxValue;
            yMin = double.MaxValue;
            xMax = double.MinValue;
            yMax = double.MinValue;
            zMin = double.MaxValue;
            zMax = double.MinValue;
            mMin = double.MaxValue;
            mMax = double.MinValue;
        }

        public void Read(ShapeBinaryReader binaryReader)
        {
            xMin = binaryReader.ReadDouble();
            yMin = binaryReader.ReadDouble();
            xMax = binaryReader.ReadDouble();
            yMax = binaryReader.ReadDouble();
            zMin = binaryReader.ReadDouble();
            zMax = binaryReader.ReadDouble();
            mMin = binaryReader.ReadDouble();
            mMax = binaryReader.ReadDouble();
        }

        public void Write(ShapeBinaryWriter binaryWriter)
        {
            binaryWriter.Write(xMin);
            binaryWriter.Write(yMin);
            binaryWriter.Write(xMax);
            binaryWriter.Write(yMax);
            binaryWriter.Write(zMin);
            binaryWriter.Write(zMax);
            binaryWriter.Write(mMin);
            binaryWriter.Write(mMax);
        }

        /// <summary>
        /// merge boundingbox means return max rect from two boundingbox
        /// </summary>
        /// <param name="boundingBox"></param>
        public void Merge(BoundingBox boundingBox)
        {
            xMin = xMin < boundingBox.xMin ? xMin : boundingBox.xMin;
            yMin = yMin < boundingBox.yMin ? yMin : boundingBox.yMin;
            xMax = xMax > boundingBox.xMax ? xMax : boundingBox.xMax;
            yMax = yMax > boundingBox.yMax ? yMax : boundingBox.yMax;
            zMin = zMin < boundingBox.zMin ? zMin : boundingBox.zMin;
            zMax = zMax > boundingBox.zMax ? zMax : boundingBox.zMax;
            mMin = mMin < boundingBox.mMin ? mMin : boundingBox.mMin;
            mMax = mMax > boundingBox.mMax ? mMax : boundingBox.mMax;
        }
    }
}
